package com.file.service.impl;

import com.file.dao.PicDao;
import com.file.entity.Pic;
import com.file.service.PicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Service
public class PicServiceImpl implements PicService {

    @Autowired
    private PicDao picDao;

    @Override
    public List<Pic> selectAllPic() {
        return picDao.selectAllPic();
    }

    @Override
    public int uploadFile(MultipartFile multipartFile) {
        Pic pic=new Pic();
        try {
            if(multipartFile!=null){

                pic.setContent(multipartFile.getBytes());
                pic.setFilename(multipartFile.getOriginalFilename());
                pic.setFileSize(new BigDecimal(multipartFile.getSize()));
                pic.setContentType(multipartFile.getContentType());
                pic.setPicAddress(multipartFile.getOriginalFilename());
                int updated=picDao.save(pic);
            }else {
                System.out.println("没有文件");
            }
            Files.write(Paths.get("D:","/ps/upload/",multipartFile.getOriginalFilename()),multipartFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
     return 1;
    }

    @Override
    public List<Pic> list() {
        return picDao.selectAllPic();
    }

    @Override
    public Pic find(String id) {
        return picDao.findOne(id);
    }
}
